/**
 * @file ipeLenetParam.h
 * @author SDK_TEAM
 * @brief
 * @version 0.1
 * @date 2022-10-26
 *
 * Copyright:
 * © 2018 北京灵汐科技有限公司 版权所有。
 * 注意：以下内容均为北京灵汐科技有限公司原创，未经本公司允许，不得转载，否则将视为侵权；对于不遵守此声明或者其他违法使用以下内容者，本公司依法保留追究权。
 * © 2018 Lynxi Technologies Co., Ltd. All rights reserved.
 * NOTICE: All information contained here is, and remains the property of Lynxi.
 * This file can not be copied or distributed without the permission of Lynxi
 * Technologies Co., Ltd.
 *
 */

#pragma once

#include <lyn_api.h>
#include <iostream>
#include "ipeParam.h"
#include "util.hpp"

class IpeParamYolov5s6 : IpeParamBase {
 public:
  IpeParamYolov5s6(int iModelWidth, int iModelHeight)
      : IpeParamBase(iModelWidth, iModelHeight) {}
  ~IpeParamYolov5s6() {}

  // IPE处理
  virtual void CalcParam(lynStream_t stream, void *inputData,
                         void *outputData) {
    // 结构体reset
    CHECK_ERR(lynIpeResetPicDesc(m_oPicDescIn));
    CHECK_ERR(lynIpeResetPicDesc(m_oPicDescOut));
    CHECK_ERR(lynIpeResetConfigDesc(m_oConfigDesc));
    CHECK_ERR(lynIpeSetInputPicDesc(m_oPicDescIn, inputData, m_iImgWidth,
                                    m_iImgHeight, m_format));
    CHECK_ERR(lynIpeSetOutputPicData(m_oPicDescOut, outputData));
    CHECK_ERR(lynIpeSetC2CConfig(m_oConfigDesc, LYN_PIX_FMT_RGB24, 0));
    CHECK_ERR(
        lynIpeSetResizeConfig(m_oConfigDesc, m_iResizeWidth, m_iResizeHeight));
    CHECK_ERR(
        lynIpeCalOutputPicDesc(m_oPicDescOut, m_oPicDescIn, m_oConfigDesc, 0));
    CHECK_ERR(
        lynIpeProcessAsync(stream, m_oPicDescIn, m_oPicDescOut, m_oConfigDesc));
  }

  virtual void SetImgInfo(int32_t width, int32_t height,
                          lynPixelFormat_t format) {
    double fScaleW = (double)m_iModelWidth / (double)width;
    double fScaleH = (double)m_iModelHeight / (double)height;
    double fScale = std::min(fScaleW, fScaleH);
    m_iResizeWidth = static_cast<int>(((double)width * fScale)) & ~1;
    m_iResizeHeight = static_cast<int>(((double)height * fScale)) & ~1;

    if (m_iResizeWidth > 1920 || m_iResizeHeight > 1080) {
      fScaleW = (double)1920 / (double)m_iResizeWidth;
      fScaleH = (double)1080 / (double)m_iResizeHeight;
      fScale = fScale * std::min(fScaleW, fScaleH);
      m_iResizeWidth = static_cast<int>(((double)width * fScale)) & ~1;
      m_iResizeHeight = static_cast<int>(((double)height * fScale)) & ~1;
    }
    IpeParamBase::SetImgInfo(width, height, format);
  }

 private:
  // 图片resize参数
  int m_iResizeWidth = 0;
  int m_iResizeHeight = 0;
};
